﻿using System;
using System.Linq;
using System.Reflection;
using Castle.Windsor;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.MicroKernel.Registration;
using GroceryList.Model.Repositories;

namespace GroceryList.Front.Web.Installers
{
    public class RepositoryInstaller :IWindsorInstaller
    {
 
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            var tipos = Assembly.GetAssembly(typeof(ProductoRepository)).GetTypes();
            var tipoRepo = tipos.Where(tipo => tipo.FindInterfaces(RepositoryTypeFilter, "Repository").Count() > 0).ToList();
            foreach (var tipo in tipoRepo)
            {
                container.Register
                (
                    Component.For(tipo.FindInterfaces(RepositoryTypeFilter, "Repository").First()).
                    ImplementedBy(tipo)
                );

            }
        }

        private static bool RepositoryTypeFilter(Type m, object filtercriteria)
        {
            return m.ToString().EndsWith(filtercriteria.ToString());
        }

    }
}